package com.wsz.ucenter.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsz.pojo.AclUser;
import com.wsz.pojo.R;
import com.wsz.pojo.Ucenter;
import com.wsz.pojo.query.EssayQuery;
import com.wsz.ucenter.fegin.UserFeginClient;
import com.wsz.ucenter.service.AclUserService;
import com.wsz.ucenter.service.UcenterService;
import com.wsz.ucenter.utils.RedisUtils;
import com.wsz.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;


@RestController
public class UcenterController {

    @Autowired
    private AclUserService aclUserService;

    @Autowired
    private UserFeginClient userFeginClient;

    @Autowired
    private UcenterService ucenterService;

    @Autowired
    private RedisUtils redisUtils;

    @PostMapping("/login")
    public R login(@RequestBody AclUser aclUser) {
        return aclUserService.login(aclUser);
    }

    @GetMapping("/get/acluser")
    public R getAclUserById(@RequestParam String id) {
        Ucenter ucenter = ucenterService.getById(id);
        return R.ok().data("ucenter", ucenter);
    }

    //获取前台用户列表
    @GetMapping("/get/users/{page}/{limit}")
    public R userList(@PathVariable Long page,
                      @PathVariable Long limit) {
        return userFeginClient.userList(page, limit);
    }

    @GetMapping("/getinfo")
    public R getInfo(@RequestParam String id) {
        AclUser userInfo = aclUserService.getById(id);
        return R.ok().data("userinfo", userInfo);
    }


    //封禁用户
    @PostMapping("/update/enabled")
    public R updateEnabled(@RequestParam String userId, @RequestParam Boolean enabled) {
        if (enabled == null || userId == null) return R.error().message("参数错误");
        userFeginClient.updateEnabled(userId, enabled);
        return R.ok();
    }

    //注册用户的人数和pk人数
    public Ucenter getUcenterCount(@RequestParam String date) {
        R r = userFeginClient.getUcenterCount(date);
        Ucenter ucenter = JSONObject.parseObject(JSON.toJSONString(r.getData().get("ucenter")), Ucenter.class);
        System.out.println(ucenter);
        ucenterService.remove(new LambdaQueryWrapper<Ucenter>().eq(Ucenter::getDate, ucenter.getDate()));
        ucenterService.save(ucenter);
        return ucenter;
    }


    @GetMapping("/yesterday/count")
    public R getYesterDayCount() {
        String s = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        Ucenter ucenter = ucenterService.getOne(new LambdaQueryWrapper<Ucenter>().eq(Ucenter::getDate, s));
        if (ucenter == null) {
            ucenter = this.getUcenterCount(s);
        }
        return R.ok().data("ucenter", ucenter);
    }


    @GetMapping("/statistics")
    private R statistics(@RequestParam String begin,
                         @RequestParam String end,
                         @RequestParam String type) {
        Map<String, Object> data = ucenterService.statisticsByTime(begin, end, type);
        return R.ok().data(data);
    }

    @PostMapping("/get/essays/{userId}/{page}/{limit}")
    public R essaysList(@PathVariable String userId,
                        @PathVariable Long page,
                        @PathVariable Long limit,
                        @RequestBody(required = false) EssayQuery essayQuery) {
        AclUser aclUser = aclUserService.getById(userId);
        if(aclUser != null) {
            String key = "acl_essay_" + userId;
            redisUtils.set(key,userId,60);
            System.out.println(essayQuery + key);
            return userFeginClient.getEaasys(userId,limit,page,essayQuery);
        }
        return R.error().message("参数非法");
    }


    @DeleteMapping("/delete/essay")
    public R deleteEssay(@RequestParam("id") String id,
                        @RequestParam("userId") String userId) {
        AclUser aclUser = aclUserService.getById(userId);
        if(aclUser != null) {
            String key = "acl_essay_delete" + userId;
            redisUtils.set(key,userId,60);
            return userFeginClient.deleteEssayAcl(id,userId);
        }
        return R.error().message("系统繁忙");
    }

    @GetMapping("/essay")
    R getEssay(@RequestParam("id") String id) {
        return userFeginClient.getEssay(id);
    }
}
